package com.lemontree.common.utils;

import java.util.Random;

/**
 * @Author: YLBG-YCY-1325
 * @Description: 验证码类（emai）
 * @Date: 2017/3/27
 */
public class CodeUtil {
    private static char[] chars = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
            'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

    /**
     * 获取英文、数字验证码
     * @param num 验证码个数
     * @return 验证码
     */
    public static String getCode(int num){
        // 拼接随机值
        Random random = new Random();
        // 随机获得数字数组大小（随机数）
        int intNum = random.nextInt(num) + 1;
        // 获取字符数组的大小
        int charNum = num - intNum;
        return nums(intNum)+chars(charNum);
    }

    /**
     * 获取len个纯数字码
     * @param len 个数
     * @return 数字字符串
     */
    public static String nums(int len){
        if (len == 0){
            return StringUtil.EMPTY;
        }
        StringBuffer stringBuffer = new StringBuffer();
        Random random = new Random();
        int i = len;
        // 获取intNum个随机数
        while (i > 0) {
            stringBuffer.append(random.nextInt(9) + 1);
            i--;
        }
        return stringBuffer.toString();
    }

    /**
     *  获取纯英文验证码
     * @param len 个数
     * @return 英文验证码
     */
    public static String chars(int len){
        if (len == 0){
            return StringUtil.EMPTY;
        }
        StringBuffer buffer = new StringBuffer();
        Random random = new Random();
        int charNum = len;
        // 随机获取charNum个随机字符
        while (charNum>0){
            int letterNum = random.nextInt(Contants.CHAR_NUM)+1;
            buffer.append(chars[letterNum]);
            charNum--;
        }
        return buffer.toString();
    }

}
